package cn.kgc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/4/19
 * @Description:
 */
@RestController
@RequestMapping("exam")
public class ExamController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate  restTemplate;

    //  访问固定节点
    @RequestMapping("m1")
    public String  method1(){
        //  访问成绩服务  score  m1    http://localhost:8503/score/m1
        //RestTemplate restTemplate = new RestTemplate();
        //String info  = restTemplate.getForObject("http://localhost:8503/score/m1", String.class);
        return "exam m1 ok  score controller response result";
    }


    //  访问随机节点   随机策略
    @RequestMapping("m2")
    public String  method2(){
        //  访问成绩服务  score  m1    http://localhost:8503/score/m1   http://localhost:8504/score/m1   http://publocalhost:8505/score/m1
        //RestTemplate restTemplate = new RestTemplate();
        //String info  = restTemplate.getForObject("http://"+getHost()+"/score/m1", String.class);
        return "exam m2 ok  score controller response result = ";
    }

    public String getHost(){
        List<String> hosts = Arrays.asList("localhost:8503", "localhost:8504", "localhost:8505");
        Random random = new Random();
        return  hosts.get(random.nextInt(hosts.size()));
    }

    //   discoveryClient
    @RequestMapping("m3")
    public String  method3(){

        // 从注册中心 根据服务的名字拉取通信节点实例
        List<ServiceInstance> scoreclient = discoveryClient.getInstances("SCORECLIENT");
        for (ServiceInstance serviceInstance : scoreclient) {
            System.out.println("serviceInstance.getUri() = " + serviceInstance.getUri());
        }

        // 创建发送http请求的对象  restTemplate
        RestTemplate restTemplate = new RestTemplate();
        // 从节点列表中随机选择一个实例进行通信
        ServiceInstance serviceInstance = scoreclient.get(new Random().nextInt(scoreclient.size()));
        URI uri = serviceInstance.getUri();
        // 发送http请求
        String info = restTemplate.getForObject(uri + "/score/m1", String.class);
        return "exam ok  score controller response result = " +info;

    }


    // LoadBalancerClient
    @RequestMapping("m4")
    public String  method4(){   //  RestTemplate + Ribbon

        // 从注册中心获取一个通信节点实例  轮循   IRule  ->  RoundRobinRule
        ServiceInstance scoreclient = loadBalancerClient.choose("SCORECLIENT");
        URI uri = scoreclient.getUri();

        RestTemplate restTemplate = new RestTemplate();
        String info = restTemplate.getForObject(uri + "/score/m1", String.class);
        return "exam ok  score controller response result = " +info ;

    }


    @RequestMapping("m5")
    public String  method5(){
        // 使用spring容器中具有负载均恒能力的restTemplate 实现通信 http://SCORECLIENT/score/m1  轮循
        String info  = restTemplate.getForObject("http://SCORECLIENT/score/m1", String.class);
        return "exam ok  score controller response result = " +info;

    }


}
